<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>object</title>
<link rel="stylesheet" href="../../../../../../doc/src/boostbook.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
<link rel="home" href="../../index.html" title="Chapter 1. Boost.JSON">
<link rel="up" href="../dom.html" title="Document Model">
<link rel="prev" href="array.html" title="array">
<link rel="next" href="numbers.html" title="Numbers">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table cellpadding="2" width="100%"><tr>
<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../../boost.png"></td>
<td align="center"><a href="../../../../../../index.html">Home</a></td>
<td align="center"><a href="../../../../../../libs/libraries.htm">Libraries</a></td>
<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
<td align="center"><a href="../../../../../../more/index.htm">More</a></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="array.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../dom.html"><img src="../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="numbers.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="json.dom.object"></a><a class="link" href="object.html" title="object">object</a>
</h3></div></div></div>
<p>
        A <a class="link" href="../ref/boost__json__value.html" title="value"><code class="computeroutput"><span class="identifier">value</span></code></a>
        stores an instance of <a class="link" href="../ref/boost__json__object.html" title="object"><code class="computeroutput"><span class="identifier">object</span></code></a> as the underlying representation
        for a JSON object . Instances of the <a class="link" href="../ref/boost__json__object.html" title="object"><code class="computeroutput"><span class="identifier">object</span></code></a> type are associative containers
        holding key and value pairs, where the key is a <a class="link" href="../ref/boost__json__string_view.html" title="string_view"><code class="computeroutput"><span class="identifier">string_view</span></code></a> and the mapped type
        is a <a class="link" href="../ref/boost__json__value.html" title="value"><code class="computeroutput"><span class="identifier">value</span></code></a>.
        These containers are modelled after standard maps with these properties:
      </p>
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
<li class="listitem">
            The elements are stored contiguously as instances of <a class="link" href="../ref/boost__json__key_value_pair.html" title="key_value_pair"><code class="computeroutput"><span class="identifier">key_value_pair</span></code></a>.
          </li>
<li class="listitem">
            Iterators are ordinary pointers, and may become invalidated on insertions
            and removals.
          </li>
<li class="listitem">
            The order of insertions is preserved, as long as there are no removals.
          </li>
<li class="listitem">
            All inserted values will use the same <a class="link" href="../ref/boost__json__memory_resource.html" title="memory_resource"><code class="computeroutput"><span class="identifier">memory_resource</span></code></a> as the container
            itself.
          </li>
</ul></div>
<p>
        An empty object may be constructed without incurring any memory allocations
        using the default memory resource. A <a class="link" href="../ref/boost__json__storage_ptr.html" title="storage_ptr"><code class="computeroutput"><span class="identifier">storage_ptr</span></code></a> can also be explicitly
        specified:
      </p>
<pre class="programlisting"><span class="identifier">object</span> <span class="identifier">obj1</span><span class="special">;</span> <span class="comment">// empty object, uses the default memory resource</span>

<span class="identifier">object</span> <span class="identifier">obj2</span><span class="special">(</span> <span class="identifier">make_shared_resource</span><span class="special">&lt;</span><span class="identifier">monotonic_resource</span><span class="special">&gt;()</span> <span class="special">);</span> <span class="comment">// empty object, uses a counted monotonic resource</span>
</pre>
<p>
        Initializer lists consisting of two-element key value pairs can be used to
        construct objects with initial contents. These constructors may allocate
        memory and throw:
      </p>
<pre class="programlisting"><span class="identifier">object</span> <span class="identifier">obj</span><span class="special">(</span> <span class="special">{{</span><span class="string">"key1"</span><span class="special">,</span> <span class="string">"value1"</span> <span class="special">},</span> <span class="special">{</span> <span class="string">"key2"</span><span class="special">,</span> <span class="number">42</span> <span class="special">},</span> <span class="special">{</span> <span class="string">"key3"</span><span class="special">,</span> <span class="keyword">false</span> <span class="special">}}</span> <span class="special">);</span>
</pre>
<p>
        Alternatively, elements may be inserted after construction:
      </p>
<pre class="programlisting"><span class="identifier">object</span> <span class="identifier">obj</span><span class="special">;</span>

<span class="identifier">obj</span><span class="special">.</span><span class="identifier">emplace</span><span class="special">(</span> <span class="string">"key1"</span><span class="special">,</span> <span class="string">"value1"</span> <span class="special">);</span>
<span class="identifier">obj</span><span class="special">.</span><span class="identifier">emplace</span><span class="special">(</span> <span class="string">"key2"</span><span class="special">,</span> <span class="number">42</span> <span class="special">);</span>
<span class="identifier">obj</span><span class="special">.</span><span class="identifier">emplace</span><span class="special">(</span> <span class="string">"key3"</span><span class="special">,</span> <span class="keyword">false</span> <span class="special">);</span>
</pre>
<p>
        Similar to the <code class="computeroutput"><span class="identifier">std</span></code> counterpart,
        elements may be accessed directly by their key with bounds checking using
        <a class="link" href="../ref/boost__json__object/at/overload1.html" title="object::at (1 of 2 overloads)"><code class="computeroutput"><span class="identifier">at</span></code></a>,
        or without bounds checking using <a class="link" href="../ref/boost__json__object/operator_lb__rb_.html" title="object::operator[]"><code class="computeroutput"><span class="keyword">operator</span><span class="special">[]</span></code></a>
        which creates a null element if the key does not already exist:
      </p>
<pre class="programlisting"><span class="identifier">object</span> <span class="identifier">obj</span><span class="special">;</span>

<span class="identifier">obj</span><span class="special">[</span><span class="string">"key1"</span><span class="special">]</span> <span class="special">=</span> <span class="string">"value1"</span><span class="special">;</span>
<span class="identifier">obj</span><span class="special">[</span><span class="string">"key2"</span><span class="special">]</span> <span class="special">=</span> <span class="number">42</span><span class="special">;</span>
<span class="identifier">obj</span><span class="special">[</span><span class="string">"key3"</span><span class="special">]</span> <span class="special">=</span> <span class="keyword">false</span><span class="special">;</span>

<span class="comment">// The following line throws std::out_of_range, since the key does not exist</span>
<span class="identifier">obj</span><span class="special">.</span><span class="identifier">at</span><span class="special">(</span> <span class="string">"key4"</span> <span class="special">);</span>
</pre>
<p>
        Internally, the container computes a hash table over the keys so that the
        complexity of lookups is in constant time, on average.
      </p>
<p>
        For the complete listing of all available member functions and nested types,
        see the reference page for <a class="link" href="../ref/boost__json__object.html" title="object"><code class="computeroutput"><span class="identifier">object</span></code></a>.
      </p>
<p>
        As with <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">pair</span></code>, the <a class="link" href="../ref/boost__json__key_value_pair.html" title="key_value_pair"><code class="computeroutput"><span class="identifier">key_value_pair</span></code></a> type can be used
        with structured bindings in C++17. Specializations of <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">tuple_size</span></code>,
        <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">tuple_element</span></code>, and overloads of <a class="link" href="../ref/boost__json__get.html" title="get"><code class="computeroutput"><span class="identifier">get</span></code></a> are all provided for this purpose.
      </p>
<h5>
<a name="json.dom.object.h0"></a>
        <span class="phrase"><a name="json.dom.object.formatted_output"></a></span><a class="link" href="object.html#json.dom.object.formatted_output">Formatted
        Output</a>
      </h5>
<p>
        When an <a class="link" href="../ref/boost__json__object.html" title="object"><code class="computeroutput"><span class="identifier">object</span></code></a>
        is formatted to a <a href="https://en.cppreference.com/w/cpp/io/basic_ostream" target="_top"><code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">ostream</span></code></a>,
        the result is a valid JSON. That is, the object will be output with curly
        braces and a comma separated list of key/value pairs, as per the JSON specification.
      </p>
</div>
<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
<td align="left"></td>
<td align="right"><div class="copyright-footer">Copyright © 2019, 2020 Vinnie Falco<br>Copyright © 2020 Krystian Stasiowski<p>
        Distributed under the Boost Software License, Version 1.0. (See accompanying
        file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>)
      </p>
</div></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="array.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../dom.html"><img src="../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="numbers.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a>
</div>
</body>
</html>
